package com.umeox.base

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import java.util.*

class BaseDialog : Dialog {
    /**
     * 内容View
     */
    private var customContentView: View? = null

    /**
     * 透明背景
     */
    private val mBgTransparent = ColorDrawable(Color.parseColor("#00000000"))

    /**
     * 圆角背景
     */
    private var gradientDrawable: GradientDrawable? = null

    constructor(attribute: CustomDialogParams, context: Context) : super(context) {
        initDialogParams(attribute, context)
    }

    constructor(attribute: CustomDialogParams, context: Context, themeResId: Int) : super(
        context,
        themeResId
    ) {
        initDialogParams(attribute, context)
    }

    constructor(
        attribute: CustomDialogParams,
        context: Context,
        cancelable: Boolean,
        cancelListener: DialogInterface.OnCancelListener?
    ) : super(context, cancelable, cancelListener) {
        initDialogParams(attribute, context)
    }

    /**
     * 初始化的操作
     */
    private fun initDialogParams(attribute: CustomDialogParams, context: Context) {
        //生产内容背景
        buildGradientDrawable(attribute)

        //解决Dialog无法设置宽度布满屏幕的问题
        //设置背景透明
        if (attribute.mIsBgTransparent) {
            window!!.setBackgroundDrawable(mBgTransparent)
        }
        //设置内容
        if (attribute.mLayoutParams != null) {
            setContentView(attribute.mContentView!!, attribute.mLayoutParams)
        } else {
            setContentView(attribute.mContentView!!)
        }
        customContentView = attribute.mContentView
        customContentView!!.background = gradientDrawable
        //设置宽高
        //窗口属性
        val mWindowAttributes = window!!.attributes
        if (attribute.mWindowSize != null) {
            if (attribute.mWindowSize!!.x > 0) {
                mWindowAttributes.width = attribute.mWindowSize!!.x
            }
            if (attribute.mWindowSize!!.y > 0) {
                mWindowAttributes.height = attribute.mWindowSize!!.y
            }
        }

        //设置显示位置
        window!!.setGravity(attribute.mGravity)
        //设置窗口偏移
        if (attribute.mWindowTranslate != null) {
            mWindowAttributes.x = attribute.mWindowTranslate!!.x
            mWindowAttributes.y = attribute.mWindowTranslate!!.y
        }
        //设置背景透明
        if (attribute.mIsBgTransparent) {
            window!!.setBackgroundDrawable(mBgTransparent)
        }
        //设置外圈透明度
        window!!.setDimAmount(attribute.mOutSideAlpha)
        if (attribute.mOutSideAlpha <= 0) {
            window!!.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        }

        //设置出入动画
        if (attribute.mAnimationsId != 0) {
            window!!.setWindowAnimations(attribute.mAnimationsId)
        }
    }

    private fun buildGradientDrawable(attribute: CustomDialogParams) {
        //设置圆角
        gradientDrawable = GradientDrawable()
        gradientDrawable!!.setColor(Color.TRANSPARENT)
        gradientDrawable!!.cornerRadii = attribute.mRadius
        gradientDrawable!!.setStroke(attribute.mStrokeWidth, attribute.mStrokeColor)
    }

    private fun getViewById(id: Int): View {
        return customContentView!!.findViewById(id)
    }

    class CustomDialogParams {
        val mRadius = FloatArray(8)
        var mWindowSize: Point? = null
        var mWindowTranslate: Point? = null
        var mGravity = -1
        var mIsBgTransparent = true
        var mOutSideAlpha = 1.0f
        var mAnimationsId = 0
        var mStrokeColor = -0x1
        var mStrokeWidth = 0
        var mContentView: View? = null
        var mLayoutParams: ViewGroup.LayoutParams? = null

        /**
         * 设置的单位为 px ** 不是dp **
         */
        fun setCorners(radius: Float): CustomDialogParams {
            Arrays.fill(mRadius, radius)
            return this
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        fun setCorners(
            leftTopRadius: Float,
            rightTopRadius: Float,
            leftBottomRadius: Float,
            rightBottomRadius: Float
        ): CustomDialogParams {
            mRadius[1] = leftTopRadius
            mRadius[0] = mRadius[1]
            mRadius[3] = rightTopRadius
            mRadius[2] = mRadius[3]
            mRadius[5] = rightBottomRadius
            mRadius[4] = mRadius[5]
            mRadius[7] = leftBottomRadius
            mRadius[6] = mRadius[7]
            return this
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        fun setCorners(
            leftTopRadius_x: Float, leftTopRadius_y: Float,
            rightTopRadius_x: Float, rightTopRadius_y: Float,
            leftBottomRadius_x: Float, leftBottomRadius_y: Float,
            rightBottomRadius_x: Float, rightBottomRadius_y: Float
        ): CustomDialogParams {
            mRadius[0] = leftTopRadius_x
            mRadius[1] = leftTopRadius_y
            mRadius[2] = rightTopRadius_x
            mRadius[3] = rightTopRadius_y
            mRadius[4] = leftBottomRadius_x
            mRadius[5] = leftBottomRadius_y
            mRadius[6] = rightBottomRadius_x
            mRadius[7] = rightBottomRadius_y
            return this
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        fun setWindowSize(windowWidth: Int, windowHeight: Int): CustomDialogParams {
            if (mWindowSize == null) {
                mWindowSize = Point()
            }
            mWindowSize!!.x = windowWidth
            mWindowSize!!.y = windowHeight
            return this
        }

        /**
         * ex: Gravity.CENTER
         */
        fun setGravity(gravity: Int): CustomDialogParams {
            mGravity = gravity
            return this
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        fun setWindowTranslate(x: Int, y: Int): CustomDialogParams {
            if (mWindowTranslate == null) {
                mWindowTranslate = Point()
            }
            mWindowTranslate!!.x = x
            mWindowTranslate!!.y = y
            return this
        }

        /**
         * 设置window背景是否透明
         */
        fun setBgTransparent(isBgTransparent: Boolean): CustomDialogParams {
            mIsBgTransparent = isBgTransparent
            return this
        }

        /**
         * 设置弹窗外圈透明度
         */
        fun setBgOutSizeAlpha(alpha: Float): CustomDialogParams {
            mOutSideAlpha = alpha
            return this
        }

        /**
         * 设置动画
         * ex: values/style.xml
         * <style name="DialogTransAnim">
        <item name="android:windowEnterAnimation">@anim/trans_enter_anim</item>
        <item name="android:windowExitAnimation">@anim/trans_exit_anim</item>
        </style>
         * id = R.style.DialogTransAnim
         */
        fun setAnimations(id: Int): CustomDialogParams {
            mAnimationsId = id
            return this
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        fun setStrokeColorAndWidth(width: Int, color: Int): CustomDialogParams {
            mStrokeColor = color
            mStrokeWidth = width
            return this
        }

        /**
         * 设置内容
         */
        fun contentView(layoutResID: Int, context: Context?): CustomDialogParams {
            mContentView = LayoutInflater.from(context).inflate(layoutResID, null, false)
            return this
        }

        /**
         * 设置内容
         */
        fun contentView(contentView: View, params: ViewGroup.LayoutParams?): CustomDialogParams {
            mContentView = contentView
            mLayoutParams = params
            return this
        }

        /**
         * 设置内容
         */
        fun contentView(contentView: View?): CustomDialogParams {
            mContentView = contentView
            return this
        }

        fun build(context: Context): BaseDialog {
            return create(this, context)
        }

        fun build(context: Context, themeResId: Int): BaseDialog {
            return create(this, context, themeResId)
        }

        fun build(
            context: Context,
            cancelable: Boolean,
            cancelListener: DialogInterface.OnCancelListener?
        ): BaseDialog {
            return create(this, context, cancelable, cancelListener)
        }
    }

    companion object {
        private fun create(attribute: CustomDialogParams, context: Context): BaseDialog {
            return BaseDialog(attribute, context)
        }

        private fun create(
            attribute: CustomDialogParams,
            context: Context,
            themeResId: Int
        ): BaseDialog {
            return BaseDialog(attribute, context, themeResId)
        }

        private fun create(
            attribute: CustomDialogParams,
            context: Context,
            cancelable: Boolean,
            cancelListener: DialogInterface.OnCancelListener?
        ): BaseDialog {
            return BaseDialog(attribute, context, cancelable, cancelListener)
        }
    }
}